package com.problem.stack;

import java.util.ArrayList;
import java.util.List;

class MinStack {

    private Node top;

    public MinStack() {
    }
    
    public void push(int val) {
        Node head = new Node(val);
        if (top != null) {
            head.next = top;
        }
        top =  head;
    }
    
    public void pop() {
        if (top != null) {
            top = top.next;
        }
    }
    
    public int top() {
        return top == null?0:top.value;
    }
    
    public int getMin() {
        if (top == null) {
            return Integer.MIN_VALUE;
        }
        int min = top.value;
        Node next = top;
        while ((next = next.next) != null) {
            min = Math.min(min, next.value);
        }
        return min;
    }

    static class Node{
        private final int value;
        private Node next;
        public Node(int value) {
            this.value = value;
        }
    }

//    private List<Integer> arr;
//
//    public MinStack() {
//        arr = new ArrayList<>();
//    }
//
//    public void push(int val) {
//        arr.add(val);
//    }
//
//    public void pop() {
//        arr.removeLast();
//    }
//
//    public int top() {
//        return arr.getLast();
//    }
//
//    public int getMin() {
//        int min = Integer.MAX_VALUE;
//        for (int i = 0; i < arr.size(); i++) {
//            min = arr.get(i) < min ? arr.get(i) : min;
//        }
//        return min;
//    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */